package test;

import java.util.Scanner;

public class Dfs {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        int a = scan.nextInt();
        int b = scan.nextInt();
        int[][] my = new int[a][b];
        int[][] visited = new int[a][b];
        int[][] ans = new int[a][b];
        for (int i = 0; i < a; i++) {
            for (int j = 0; j < b; j++) {
                my[i][j] = scan.nextInt();
                ans[i][j] = Integer.MAX_VALUE;
            }
        }
        int startx = scan.nextInt() - 1;
        int starty = scan.nextInt() - 1;
        int endx = scan.nextInt() - 1;
        int endy = scan.nextInt() - 1;
        visited[startx][starty] = 1;
        ans[startx][starty] = 0;
        dfs(a,b,startx, starty, endx, endy, my,ans);
        if(ans[endx][endy] != Integer.MAX_VALUE){
            System.out.println(ans[endx][endy]);
        }else{
            System.out.println(-1);
        }
        scan.close();
    }
    private static void dfs(int rows,int cows, int nx, int ny, int endx, int endy, int[][] my, int[][] ans) {
        int[] fx = {1, 0, -1, 0};
        int[] fy = {0, 1, 0, -1};
        if (nx == endx && ny == endy) {
            return;
        }
        for(int k = 0; k < 4; k++) {
            int tx = nx + fx[k];
            int ty = ny + fy[k];
            if( rows<=tx ||tx<0 || cows<=ty || ty<0){
                continue;
            }
            if (my[tx][ty] == 1 &&ans[tx][ty] > ans[nx][ny] + 1) {

                ans[tx][ty] =ans[nx][ny] + 1;
                dfs(rows, cows, tx, ty, endx, endy, my,  ans);

            }
        }
    }
}
